﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery uploader
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.异步上传
2.
3.
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    $.fn.uploader = function (options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>               
        options = $.extend({
            handler: iconfig.handles.upload
        }, options);
        var id = this.attr('id');

        var form = $('<form name="uploadform" method="post" enctype="multipart/form-data" />').attr('id', id + '_form').attr('name', id + '_form')
        .attr('action', options.handler).attr('target', id + '_if');
        var file = $("<input type='file' name='" + id + "_file' />").attr('id', id + '_file').addClass('txt file').attr('contenteditable', 'false').appendTo(form);
        $("<input type='hidden' name='hidUPFName' />").attr('id', id + '_hid').attr('field', 'OldPath').appendTo(form);
        $("<iframe name='" + id + "_if' />").attr('id', id + '_if').hide().appendTo(form);
        file.blur(function () {
            var tfile = $(this), name = tfile.val();
            if (name.length > 0) {
                $('#' + tfile.attr('id').replace('_file', '')).val(name);
            }
        });
        this.hide().after(form);
        return file;
    };

    ///#region 公共设置方法
    ///#endregion

    ///#region 公共方法
    ///#endregion

})(jQuery);
function upload_file(id, container, fnend, param) {
    if (param && $(id + '_file').val().length > 0) {
        $(id + '_hid').val(param);

        if (container)
            ictrcore.loading(container);
        $(id + '_if').load(function () {
            var result = "0",
            doc = this.contentDocument || frames[this.id].document;
            if (doc.body)
                result = doc.body.innerHTML;

            switch (result) {
                case "0"://上传失败
                    $icontrol.alert($iconst.Common.upload);
                    break;
                case "2"://文件过大
                    $icontrol.alert($iconst.Common.uploadsize);
                    break;
            }
            ictrcore.loadingClose(container);
            if (fnend)
                fnend(result == "1");
        });
        $(id + '_form').submit();
    }
    else if (fn)
        fn(true);
}